package com.zq.server.security.filter;


import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import com.zq.common.core.context.BaseContext;
import com.zq.common.core.domain.model.LoginUser;
import com.zq.common.core.domain.model.WeChatLoginUser;
import com.zq.common.utils.SecurityUtils;
import com.zq.common.utils.StringUtils;
import com.zq.server.core.sevcice.TokenService;
import com.zq.server.core.sevcice.weChatTokenService;

/**
 * token过滤器 验证token有效性
 */
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private weChatTokenService weChatTokenServices;


    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException {
         String token = weChatTokenServices.getToken(request);
         String adminToken = tokenService.getToken(request);
         if (StringUtils.isNotEmpty(token)) {
            //app端逻辑
             WeChatLoginUser weChatUser = weChatTokenServices.getWeChatUser(token);
            if (StringUtils.isNotNull(weChatUser)) {
                //TODO
                BaseContext.setCurrentId(weChatUser.getUserId());
                weChatTokenServices.verifyToken(weChatUser);
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(weChatUser, null, null);
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }
        }
         if (StringUtils.isNotEmpty(adminToken)) {
             //管理端端逻辑
             LoginUser loginUser = tokenService.getLoginUser(request);
             if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication())) {
                 tokenService.verifyToken(loginUser);
                 UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
                 authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                 SecurityContextHolder.getContext().setAuthentication(authenticationToken);
             }
           }
        chain.doFilter(request, response);
    }
}
